<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal, 
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				isTrue:false,
				editId:''
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				if(_that.checkedit){
					_that.om.formSave("commonForm/doCheckOrApproveUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("tpromoteEvaluateMulti/addTpromoteEvaluateMulti",_that.formdata,_that)
				}
			},
			getWorkParam(){
				let _that = this
				return {
						zposId:_that.$refs.autoform.formdata.zposId,
						isTrue:_that.isTrue?_that.isTrue:'',
						editId:_that.editId
					}
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.dataDictInitValueUrl('DD014009001'),
					{
						url:"wxPosBusiAuth/defaultTableMaker",
						param: {
							code: options.code
						},
						method: "post"
					}
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"tpromoteEvaluateMulti/getInfo",
						param:{
							id:fileId
						},
						method:'post'
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					var defaultPosId = res[2].value.id || '';
					var defaultPosName = res[2].value.posName || '';
					var defaultInstId = res[2].value.instId || '';
					var defaultInstName = res[2].value.instName || '';
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					if(res[1].value = 1){
						_that.isTrue = true
					}
					if(_that.subEditorObj['title_salary']){
						let att = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData['title_salary']
						if(hasEditor("title_salary","userId",configData)){
							att.titleTemplate = "<%= userName %>"
						}
						changeEditor("title_salary","dataStr",att,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						_that.editId = fileId
						fieldArr.push('id')
						for(let index in res[3].value.tpromoteEvaluateMulti){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[3].value.tpromoteEvaluateMulti[index])
								_that.$set(_that.formdata,index,res[3].value.tpromoteEvaluateMulti[index])
							}
						}
						if(_that.subEditorObj["title_salary"]){
							let dataList = _that._.cloneDeep(res[3].value.dataList)
							_that.subEditorObj["title_salary"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
							_that.$set(_that.formdata,"dataStr",JSON.stringify(dataList))
						}
					}
					_that.$nextTick(function(){
						// if(!_that.$refs.autoform.formdata.zposId){
						// 	_that.$refs.autoform.initSelectVal("topinfo","zposId")
						// }else{
						// 	if(!_that.checkedit){
						// 		_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						// 	}
						// }
						
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.formdata.zposId = defaultPosId
							_that.$refs.autoform.formdata.zposName = defaultPosName
							_that.$refs.autoform.formdata.zinstId = defaultInstId
							_that.$refs.autoform.formdata.zinstName = defaultInstName
							_that.$nextTick(function() {
								_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
							})
						}else{
							if(!_that.checkedit){
								_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
							}
						}
					})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
							editor.workList = res.data
							uni.removeStorage({
								key:getWorkCacheKey(fieldId)
							})
						}
					})
				}
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
